package com.gopay.bis.ofpay.impl;


import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.ofpay.operation.OfpayChargeHelper;
import com.gopay.bis.ofpay.operation.OfpayQueryHelper;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasResult;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.TpasChargeInteractRemoteService;

/**
 * @OfpayServiceImpl.java
 * @author liuzhangpeng
 * @2015年10月26日 下午3:57:11  www.gopay.com.cn Inc.All rights reserved.
 */
public class OfpayServiceImpl implements TpasChargeInteractRemoteService {
    private final static Logger logger = LoggerFactory.getLogger(OfpayQueryHelper.class);
    
    private Map<String, String> ofpayServInterface;
	private OfpayQueryHelper ofPayQueryHelper;
	private OfpayChargeHelper ofPayChargeHelper;
	
	/**
     * 手机号归属地查询
     * @param msg
     * @return
     * @throws GopayException
     */
    @Override
    public TpasResult mobileBelong(TpasMessage base) {
            setReqUrl(base);
            TpasResult result = new TpasResult(true);
            try {
                TpasMessage mobInfo = ofPayQueryHelper.mobInfo(base);
                result.setDefaultModel(mobInfo);
            } catch (Exception e) {
                result.setSuccess(false);
                result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
                result.setErrorMsg("查询失败");
                logger.error(String.format("mobile query error !! mobileNo:%s", base.getOuterCustID()), e);
            }
            return result;
    }

    @Override
    public TpasResult mobileCharge(TpasMessage base) {
    	logger.error("话费充值req", ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        setReqUrl(base);
        TpasResult result = new TpasResult(true);
        try {
            TpasMessage resMsg = ofPayChargeHelper.mobileCharge(base);
            logger.info("mobile charge result:" + ToStringBuilder.reflectionToString(resMsg, ToStringStyle.DEFAULT_STYLE));
            
            result.setDefaultModel(resMsg);
//            result.setOrderStatus(TpasMwaChargeConstants.CHARGE_SUCCESS.equals(resMsg.getRetCode()));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setOrderStatus(false);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
            result.setErrorMsg("手机充值失败");
            logger.error(String.format("mobile charge error !! mobileNo:%s,orderid:%s", base.getOuterCustID(),base.getOrderId()), e);
        }
        return result;
    }
	
	public TpasResult flowCharge(TpasMessage base) {
		logger.error("流量充值req", ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
	    setReqUrl(base);
	    TpasResult result = new TpasResult(true);
        try {
            TpasMessage resMsg = ofPayChargeHelper.flowCharge(base);
            logger.info("flow charge result:" + ToStringBuilder.reflectionToString(resMsg, ToStringStyle.DEFAULT_STYLE));
            
            result.setDefaultModel(resMsg);
//            result.setOrderStatus(TpasMwaChargeConstants.CHARGE_SUCCESS.equals(resMsg.getRetCode()));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setOrderStatus(false);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
            result.setErrorMsg("流量充值失败");
            logger.error(String.format("flow charge error !! mobileNo:%s,orderid:%s", base.getOuterCustID(),base.getOrderId()), e);
        }
        return result;
	}
	
	@Override
	public TpasResult oilCardInfo(TpasMessage base) throws GopayException {
	    setReqUrl(base);
        TpasResult result = new TpasResult(true);
        try {
            TpasMessage resMsg = ofPayQueryHelper.oilCardInfo(base);
            logger.info("query oilCard info result:" + ToStringBuilder.reflectionToString(resMsg, ToStringStyle.DEFAULT_STYLE));
            
            result.setDefaultModel(resMsg);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setOrderStatus(false);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
            result.setErrorMsg("查询失败");
            logger.error(String.format("query oilCard info  error !! cardNo:%s", base.getOuterCustID()), e);
        }
        return result;
	}
	
    public TpasResult oilCardCharge(TpasMessage base) {
    	logger.error("加油卡充值req", ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        setReqUrl(base);
        TpasResult result = new TpasResult(true);
        try {
            TpasMessage resMsg = ofPayChargeHelper.oilCardCharge(base);
            logger.info("oidCard charge result:" + ToStringBuilder.reflectionToString(resMsg, ToStringStyle.DEFAULT_STYLE));
            
            result.setDefaultModel(resMsg);
//            result.setOrderStatus(TpasMwaChargeConstants.CHARGE_SUCCESS.equals(resMsg.getRetCode()));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setOrderStatus(false);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
            result.setErrorMsg("油卡充值失败");
            logger.error(String.format("oidCard charge error !! oilCardNo:%s,orderid:%s", base.getOuterCustID(),base.getOrderId()), e);
        }
        return result;
    }

	/**
	 * 查询充值状态
	 * @param msg
	 * @return
	 * @throws GopayException
	 */
	public TpasResult queryOrderStatus(TpasMessage base) {
	    setReqUrl(base);
        TpasResult result = new TpasResult(true);
        try {
            int status = ofPayQueryHelper.queryChargeStatus(base);
            logger.info("query charge status:" + status);
            String res = "";
            switch (status) {
            case 1:
                res = TpasMwaChargeConstants.CHARGE_SUCCESS;
                break;
            case 9:
                res = TpasMwaChargeConstants.CHARGE_FAIL;
                break;
            case 0:
                res = TpasMwaChargeConstants.CHARGE_PROCESS;
                break;
            case -1:
              //欧飞没有这笔单，暂当失败处理
                res = TpasMwaChargeConstants.CHARGE_FAIL;
                break;
            default:
                throw new GopayException();
            }
            result.setDefaultModel(res);
            result.setOrderStatus(TpasMwaChargeConstants.CHARGE_SUCCESS.equals(res));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setOrderStatus(false);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100F3002);
            result.setErrorMsg("订单状态查询失败");
            logger.error(String.format("query order status error !! orderid:%s", base.getOrderId()), e);
        }
		return result;
	}

    private void setReqUrl(TpasMessage msg) {
        String url = ofpayServInterface.get(msg.getInterfaceCode().getValue());
        msg.setPayUrl(url);
    }

    public void setOfpayServInterface(Map<String, String> ofpayServInterface) {
        this.ofpayServInterface = ofpayServInterface;
    }

    public void setOfPayQueryHelper(OfpayQueryHelper ofPayQueryHelper) {
        this.ofPayQueryHelper = ofPayQueryHelper;
    }

    public void setOfPayChargeHelper(OfpayChargeHelper ofPayChargeHelper) {
        this.ofPayChargeHelper = ofPayChargeHelper;
    }

    @Override
    public TpasResult tpasBack(TpasMessage msg) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }
//    /**
//     * 将此类迁移到/eis-exten-ofpay/src/main/java/com/gopay/eis/ofpay/impl/OfpayServiceImpl.java后，
//     * 由于共用一个	TpasChargeInteractRemoteService，所以此处必须实现接口方法，防止编译报错
//     * @param base
//     * @return
//     * @throws GopayException
//     */
//    @Override
//    public TpasResult videoCharge(TpasMessage base) throws GopayException {
//        logger.error("bis_bank-ofpay视频会员卡充值req", ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
//        TpasResult result = new TpasResult(false);
//        return result;
//    }
//    
//    /**
//     * 将此类迁移到/eis-exten-ofpay/src/main/java/com/gopay/eis/ofpay/impl/OfpayServiceImpl.java后，
//     * 由于共用一个	TpasChargeInteractRemoteService，所以此处必须实现接口方法，防止编译报错
//     * @param base
//     * @return
//     * @throws GopayException
//     */
//	@Override
//	public TpasResult queryOrderInfo(TpasMessage base) throws GopayException {
//		 logger.error("bis_bank-ofpayqueryOrderInforeq", ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
//        TpasResult result = new TpasResult(false);
//        return result;
//	}
}
